package com.hzqy.service.epg;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.epg.AcIntegralDao;
import com.hzqy.web.vo.SysAreaStreetVo;
import com.hzqy.web.vo.activity.ActivityVoteUserVo;
import com.hzqy.web.vo.epg.AcIntegralVo;


@Service
public class AcIntegralService {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private AcIntegralDao acIntegralDao;

	private static String Periods() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		int now = Integer.parseInt(sdf.format(new Date()));
		String type = "1";
		if(now>=20190328&&now<20190420) {
			type = "1";
		}else if(now>=20190420&&now<20190516) {
			type = "2";
		}else if(now>=20190516) {
			type = "3";
		}
		return type;
	}
	
	public Map<String,Object> selectIntegral(AcIntegralVo aiv,int page,int rows) {
		Map<String,Object> map=new HashMap<String, Object>();
		Integer rowCount = acIntegralDao.selectIntegralCount(aiv);
		if(rowCount>0 && page>0 && rows>0){
			map.put("total", rowCount);
			map.put("aiv_serviceId", aiv.getAiv_serviceId());
			map.put("aiv_phone",aiv.getAiv_phone());
			map.put("aiv_type",Periods());
			map.put("firstResult", (page-1)*rows);
			map.put("maxResults", rows);
			List<AcIntegralVo> list=acIntegralDao.selectIntegral(map);
			map.put("rows", list);
		}else{
			map.put("rows", new ArrayList<AcIntegralVo>());
		}
		return map;
	}
	
	public Map<String,Object> selectCommunityIntegral(AcIntegralVo aiv,int page,int rows) {
		Map<String,Object> map=new HashMap<String, Object>();
		Integer rowCount = acIntegralDao.selectCommunityIntegralCount(aiv);
		if(rowCount>0 && page>0 && rows>0){
			map.put("total", rowCount);
			map.put("aiv_activity",aiv.getAiv_activity());
			map.put("aiv_sasName", aiv.getAiv_sasName());
			map.put("aiv_sasParentid", aiv.getAiv_sasParentid());
			map.put("firstResult", (page-1)*rows);
			map.put("maxResults", rows);
			List<AcIntegralVo> list=acIntegralDao.selectCommunityIntegral(map);
			for(int i = 0; i<list.size();i++) {
				list.get(i).setAiv_saName(acIntegralDao.selectById(list.get(i).getAiv_sasParentid()));
			}
			map.put("rows", list);
		}else{
			map.put("rows", new ArrayList<AcIntegralVo>());
		}
		return map;
	}
	public Map<String,Object> selectArea(AcIntegralVo aiv) {
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aiv_sasParentid", aiv.getAiv_sasParentid());
		map.put("aiv_saId", aiv.getAiv_saId());
		List<SysAreaStreetVo> list=acIntegralDao.selectArea(map);
		map.put("rows", list);
		return map;
	}
	/**
	 * 插入投票记录
	 * @param acvote
	 * @return
	 */
	public int insertACLikeVote(ActivityVoteUserVo acvote) {
		return acIntegralDao.insertACLikeVote(acvote);
	}
}
